package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.exception.LoginException;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.TbSellerService;
import com.dongdongshop.util.ShiroUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/seller")
@Controller
public class SellerController {

    private Logger logger = LoggerFactory.getLogger(SellerController.class);

    @DubboReference
    private TbSellerService tbSellerService;
    @RequestMapping("/toRegister")
    public String register(){
        return "register";
    }

    @RequestMapping("/unauthorizedUrl")
    @ResponseBody
    public String unauthorizedUrl(){
        return "没有权限";
    }

    @RequestMapping("/register")
    @ResponseBody
    public Result register(TbSeller seller){
        String s = ShiroUtils.generateSalt(6);
        String md5 = ShiroUtils.encryptPassword("MD5", seller.getPassword(), s, 3);
        seller.setPassword(md5);
        seller.setSalt(s);
        try {
            TbSeller tbSeller = tbSellerService.selectSellerById(seller.getSellerId());
            if (tbSeller == null){
                tbSellerService.insertSeller(seller);
                return Result.OK();
            }else {
                return Result.response(ResultEnum.ACCOUNT_ALREADY_EXISTS);
            }
        }catch (Exception e){
            logger.error("商家注册失败 {}",e);
            return Result.ER();
        }
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "shoplogin";
    }
    @RequestMapping("/login")
    @ResponseBody
    public Result login(String emIdTel,String password){
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(emIdTel, password);
        //执行登录方法
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            logger.error("账户不存在 {}",e);
            return Result.response(ResultEnum.NOT_FOUND_ACCOUNT);
        }catch (IncorrectCredentialsException e){
            logger.error("密码错误 {}",e);
            return Result.response(ResultEnum.FAULT_PASSWORD);
        }catch (LoginException e){
            logger.error("账户异常 {}",e);
            return Result.response(ResultEnum.ACCOUNT_EXCEPTION);

        }
        return Result.OK();
    }
    @RequestMapping("/toIndex")
    public String toIndex(){
        return "/admin/index";
    }
}
